<include file="../Public/header" />
<link href="{:APPS_URL}/w3g/_static/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
  body{background-color: #f8f8f8;}
</style>
<div id="content">
    <div id="weiba" data-title="绑定邮箱" class="panel" data-selected="true"  data-menu="msgmenu" style="padding:0;">
        <header>
            <div id="header-buttons">
                <a href="javascript:history.go(-1);">
                    <div id="back" class="header-menu-link" data-back="false">
                    </div>
                </a>
            </div>
            <h1 class="hasback">
                绑定邮箱
            </h1>
        </header>
    <div class="set-item" id="">
        <form id="doBingEmail" method="post">
         <div class="form-set">
            <dl>
                <dd><input type="text" autocomplete="off" class="text1" id="write_email" name="email" placeholder="请输入新邮箱"></dd>
            </dl>
            <dl class="noborder">
                <dd>
                  <input type="text" autocomplete="off" class="text1" name="email_code" id="write_email_code" placeholder="验证码" style="width:34%;">
                  <a class="btn_gray" email="0" onclick="send_email_msg();return false;" id="get_email_code" href="javascript:;"><span>获取验证码</span></a>
                  <div style="display:none;" id="email_msg_tip" class="txt-info">验证码已发送到您的邮箱，30分钟内输入有效，请勿泄露。</div>
                </dd>
            </dl>
         </div>
         </form>
         <div class="setBtn"><a href="javascript:;" class="btn-big" onclick="do_binding_email();">保存</a></div>
    </div>
</div>
<script type="text/javascript">
var lock_email = 0;
var timefun_email = 0;
var send_email_msg = function () {
    if (lock_email == 1) {
      return false;
    }
    var email =  $('#write_email').val().replace(/\s*/,'');
    if (email === '') {
      $.ui.showMask("邮箱地址不能为空",true);
      //$.error('邮箱地址不能为空');
      return false;
    }
    if( !(/[_a-zA-Z\d\-\.]+(@[_a-zA-Z\d\-\.]+\.[_a-zA-Z\d\-]+)+$/i.test(email)) ){
      $.ui.showMask("请输入正确的邮箱地址",true);
      //ui.error('请输入正确的邮箱地址');
      return false;
    }
    $.post(U('public/Register/isEmailAvailable'), {email:email}, function(res) {
      if (res.status) {
        if (lock_email == 1) {
          return false;
        } else {
          timenum_email = 60;
          clearInterval(timefun_email);
          lock_email = 1;
        }
        $.ajax({
            type : 'post',
            url : U('w3g/Setting/getCaptcha'),
            data : {'type': 'email', 'email':email},
            dataType : 'json',
            success : function(res){
                  if (res.status == 0) {
                    lock_email = 0;
                    $.ui.showMask(res.info,true);
                    //ui.error(res.info);
                  } else {
                    $('#email_msg_tip').show();
                    timefun_email = setInterval(timeout_email, 1000);
                    //$.ui.showMask("邮件发送成功，请耐心等待",true);
                    //ui.success('邮件发送成功，请耐心等待');
                  }
            }
        });
      } else {
        //ui.error(res.info);
        $.ui.showMask(res.info,true);
      }
    }, 'json');
    return false;
};
var do_binding_email = function () {
  var email = $('#write_email').val().replace(/\s*/,'');
  if (email === '') {
    $.ui.showMask("邮箱地址不能为空",true);
    //ui.error('邮箱地址不能为空');
    return false;
  }
  if ( !(/[_a-zA-Z\d\-\.]+(@[_a-zA-Z\d\-\.]+\.[_a-zA-Z\d\-]+)+$/i.test(email)) ) {
    //ui.error('请输入正确的邮箱地址');
    $.ui.showMask("请输入正确的邮箱地址",true);
    return false;
  }
  var email_code = $('#write_email_code').val().replace(/\s*/,'');
  if (email_code === '' || email_code.length != 4) {
    //ui.error('验证码错误，请检查验证码');
    $.ui.showMask("验证码错误，请检查验证码",true);
    return false;
  }
  $.ajax({
    type : 'post',
    url : U('w3g/Setting/doBindingEmail'),
    data: $('#doBingEmail').serialize(),
    timeout : 3000,
    dataType: 'json',
    success : function(d){
        if(d && d.status){
            $.ui.showMask(d.info,true);
            location.href=U('w3g/Setting/security');
        }
        else{
            $.ui.showMask(d.info,true);
        }
    },
    error: function(r){
        $.ui.showMask("连接服务器失败，请重试:)",true);
    }
  });
};
var timeout_email = function () {
  timenum_email -= 1;
  if (timenum_email < 0) {
    clearInterval(timefun_email);
    timefun_email = '';
    lock_email = 0;
    $('#email_msg_tip').hide();
    $('#get_email_code').find('span').text('获取验证码');
    return;
  }
  $('#get_email_code').find('span').text('('+timenum_email+')获取验证码');
};
</script>
<include file="../Public/footer" />